#include "comm/znx_log.h"
#include "conf/znx_listeners_conf.h"

int main()
{
    znx_listeners_conf_t    *conf;

    conf = znx_listeners_conf_create();

    if (!znx_listeners_conf_parse(conf, "/tmp/listeners_exp.yaml")) {
        ZNX_DEF_LOG_FATAL(NULL, "parse /tmp/listeners_exp.yaml failed, err: %v", &conf->err);
    }

    if (conf->array->nelts != 2) {
        ZNX_DEF_LOG_FATAL(NULL, "expect 2, but got %d", conf->array->nelts);
    }

    size_t                      i;
    znx_listener_conf_t         *listener_confp, *listener_conf;
    listener_confp = conf->array->elts;
    for (i = 0; i < conf->array->nelts; i++) {
        listener_conf = listener_confp + i;

        if (listener_conf->backlog != 1024) {
            ZNX_DEF_LOG_FATAL(NULL, "expect 1024, but got %d", listener_conf->backlog);
        }

        if (!listener_conf->nodelay) {
            ZNX_DEF_LOG_FATAL(NULL, "expect true, but got false");
        }

        if (!listener_conf->quicack) {
            ZNX_DEF_LOG_FATAL(NULL, "expect true, but got false");
        }

        if (!listener_conf->reuseport) {
            ZNX_DEF_LOG_FATAL(NULL, "expect true, but got false");
        }

        if (!listener_conf->reuseaddr) {
            ZNX_DEF_LOG_FATAL(NULL, "expect true, but got false");
        }

        if (listener_conf->ipv6only) {
            ZNX_DEF_LOG_FATAL(NULL, "expect false, but got true");
        }

        if (!listener_conf->ssl) {
            ZNX_DEF_LOG_FATAL(NULL, "expect true, but got false");
        }

        if (!listener_conf->http2) {
            ZNX_DEF_LOG_FATAL(NULL, "expect true, but got false");
        }

        if (i == 0) {
            if (listener_conf->listener_addr.len != 5 ||
                znx_strncmp(listener_conf->listener_addr.data, ":8888", 5) != 0)
            {
                ZNX_DEF_LOG_FATAL(NULL, "expect :8888, but got %v", &listener_conf->listener_addr);
            }

            if (listener_conf->protocol.len != 4 ||
                znx_strncmp(listener_conf->protocol.data, "http", 4) != 0)
            {
                ZNX_DEF_LOG_FATAL(NULL, "expect http, but got %v", &listener_conf->protocol);
            }
        } else {
            if (listener_conf->listener_addr.len != 8 ||
                znx_strncmp(listener_conf->listener_addr.data, "[::]8888", 8) != 0)
            {
                ZNX_DEF_LOG_FATAL(NULL, "expect [::]8888, but got %v", &listener_conf->listener_addr);
            }

            if (listener_conf->protocol.len != 3 ||
                znx_strncmp(listener_conf->protocol.data, "tcp", 3) != 0)
            {
                ZNX_DEF_LOG_FATAL(NULL, "expect tcp, but got %v", &listener_conf->protocol);
            }
        }
    }

    znx_listeners_conf_destroy(conf);
    return 0;
}